package ModelLayer;
import java.util.ArrayList;

/**
 * The Collection class contains a ArrayList of dvd.
 * 
 * @author (group 1(A)) 
 * @version (14/4-2011)
 */
public class Collection
{
    // instance variables - replace the example below with your own
    private ArrayList<DVD> dvd;
    private static Collection instance = null;

    /**
     * Constructor for objects of class Collection
     */
    private Collection()
    {
        dvd = new ArrayList<DVD>();
    }
    
    /**
     * Add a DVD to Collection
     * @param take a parameter of type dvd
     */
    public void addDVD(DVD newdvd)
    {
        dvd.add(newdvd);
    }
    
    /**
     * Delete dvd from the Collection
     * @param take an objekt of the type dvd
     */
    public void deleteDVD(int id)
    {
        DVD d = findDvd(id);
        dvd.remove(d);
    }
   /**
    * Find a dvd from the Collection
    * @param id Id of the dvd
    */
    public DVD findDvd(int id)
    {
        boolean found = false;
        int index = 0;
        DVD d = null;
        while (index < dvd.size() && !found)
        {
            if (dvd.get(index).getId() == id)
            {
                found = true;
                d = dvd.get(index);
            }
            index++;
        }
        if(!found)
        {
            System.out.println("DVD not found!!");
        }
        
        return d;
    }
    
    /**
     * Update the dvd in Collection
     * @param id Id of the dvd
     * @param title Title of the dvd
     * @param artist Artist of the dvd
     * @param releaseDate Release date of the dvd
     */
    public void updateDVD(int id, String title, String artist, int releaseDate)
    {
        DVD d = findDvd(id);
        d.setTitle(title);
        d.setArtist(artist);
        d.setReleaseDate(releaseDate);
        
    }
    
    /**
     * Lists all DVDs in the Collection 
     */
    public void listAllDVD()
    {
        System.out.println("Id:  Name: ");
        for(DVD d : dvd)
        {
            System.out.println(d.getId()+"    " + d.getTitle());
        }
    }
    
    /**
     * Creates an instance of Collection
     * @return Returns an instance of Collection
     */
    public static Collection getInstance()
    {
        if(instance == null)
        {
            instance = new Collection();
        }
        return instance;
    }
   
}
